home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / FixedPoint.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  3.6 KB  |  73 lines  |  [TEXT/KAHL]

  1. /* FixedPoint.h */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #ifndef Included_FixedPoint_h
  26. #define Included_FixedPoint_h
  27.  
  28. /* FixedPoint module depends on */
  29. /* MiscInfo.h */
  30. /* Audit */
  31. /* Debug */
  32. /* Definitions */
  33.  
  34. #define smallfixed_precision (12)
  35. typedef unsigned short smallfixedunsigned;
  36. typedef signed short smallfixedsigned;
  37. typedef signed long smallfixedextended;
  38. /* if ANSI hadn't been so stingy, then we might have had proper arithmetic (signed) */
  39. /* right shifting, but NOOOOO, that might be to INEFFICIENT if they required that... */
  40. #define smallfixed2int(x) (x / (/*NB long unnecessary*/1 << smallfixed_precision))
  41. #define int2smallfixed(x) (x * (1 << smallfixed_precision))
  42. #define smallfixed2double(x) (((double)x) / (1 << smallfixed_precision))
  43. #define double2smallfixed(x) ((smallfixedsigned)((x) * (1 << smallfixed_precision)\
  44.                     + ((x >= 0) ? 0.5 : -0.5)))
  45.  
  46. #define smallfixedmult(x,y) ((smallfixed)(((x) * (y)) + \
  47.                     (1 << (smallfixed_precision - 1)) / (1 << smallfixed_precision)))
  48.  
  49.  
  50. #define largefixed_precision (24)
  51. typedef unsigned long largefixedunsigned;
  52. typedef signed long largefixedsigned;
  53. #define largefixed2int(x) (x / (1L << largefixed_precision))
  54. #define int2largefixed(x) (x * (1L << largefixed_precision))
  55. #define largefixed2double(x) (((double)x) / (1L << largefixed_precision))
  56. #define largefixed2single(x) (((float)x) / (1L << largefixed_precision))
  57. #define double2largefixed(x) ((largefixedsigned)((x) * (1L << largefixed_precision)\
  58.                     + ((x >= 0) ? 0.5 : -0.5)))
  59.  
  60. #define largefixedmult(x,y) (double2largefixed(largefixed2double(x)\
  61.                     * largefixed2double(y)))
  62.  
  63.  
  64. #define smallfixed2largefixed(x) ((largefixedsigned)x\
  65.                     * (1L << (largefixed_precision - smallfixed_precision)))
  66. #define largefixed2smallfixed(x) ((smallfixed)(x\
  67.                     / (1L << (largefixed_precision - smallfixed_precision))))
  68.  
  69.  
  70. #define roundtonearest(x) ((long)((x >= 0) ? (x + 0.5) : (x - 0.5)))
  71.  
  72. #endif
  73.